<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/wx/page/session.jsp"%>
<c:set var="userId" value="${param.userId}" />
<c:set var="deptId" value="${param.deptId}" />
<c:set var="deptCode" value="${param.deptCode}" />
<c:set var="deptParentId" value="${param.deptParentId}" />
<c:set var="senderId" value="${param.senderId}" />
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <title>货主管理</title>
    <link rel="stylesheet" href="${ctx}/wx/css/default.css"/>
    <link rel="stylesheet" href="${ctx}/wx/css/picker.css"/>
    <script type="text/javascript" src="${ctx}/wx/js/picker.min.js"></script>
    <script type="text/javascript" src="${ctx}/wx/js/picker.data.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/UserService.js"></script>
    <script type="text/javascript" src="${ctx}/wx/js/alert.js"></script>
</head>
<body style="text-align: center;">
<form id="senderForm" name="senderForm" method="post">
    <input type="hidden" id="deptId" name="deptId" value="${deptId}" required>
    <input type="hidden" id="deptParentId" name="deptParentId" value="${deptParentId}" required>
    <input type="hidden" id="senderId" name="senderId" value="${senderId}">
    <input type="hidden" id="userId" name="userId">
    <input type="hidden" id="userType" name="userType" value="2">
    <input type="hidden" id="userState" name="userState" value="1">
    <input type="hidden" id="provinceId" name="provinceId" />
    <input type="hidden" id="cityId" name="cityId" />
    <input type="hidden" id="districtId" name="districtId" />
    <fieldset class="contact-inner">
        <p class="contact-input">
            <input type="search" id="areaName" name="areaName" placeholder="必填 省、市、区" onfocus="picker3(this)" required readonly />
        </p>
        <p class="contact-input">
            <input type="tel" id="userMobile" name="userMobile" maxlength="11" placeholder="必填 货主电话" onchange="getSender()" required>
        </p>
        <p class="contact-input">
            <input type="search" id="userName" name="userName" placeholder="必填 货主姓名" required>
        </p>
        <p class="contact-submit">
            <input type="button" value="保存货主" onclick="insertSender()">
        </p>
    </fieldset>
</form>
</body>
</html>
<script type="text/javascript">
    function picker3(input) {
        new Picker({
            //"title": '请选择',//标题(可选)
            "defaultValue": input.value,//默认值-多个以空格分开（可选）
            "data": cityData,//数据(必传)
            "keys": {
                "id": "Code",
                "value": "Name",
                "childData": "level"//最多3级联动
            },//数组内的键名称(必传，id、text、data)
            "callBack": function (value, areaId) {
                //回调函数（val为选择的值）
                input.value = value;
                var array = areaId.split(" ");
                dwr.util.setValue("provinceId", array[0]);
                dwr.util.setValue("cityId", array[1]);
                dwr.util.setValue("districtId", array[2]);
            }
        });
    }

    if("${senderId}".length > 0) {
        getSender();
    }
    function getSender() {
        var user = new User();
        user.userMobile = dwr.util.getValue("userMobile");
        var sender = new Sender();
        sender.senderId = "${senderId}";
        sender.user = user;
        UserService.selectSenderList(sender, callbackSelectSenderList);
    }
    function callbackSelectSenderList(list) {
        if (list.length > 0) {
            var sender = list[0];
            dwr.util.setValues(sender);
            dwr.util.setValues(sender.user);
            dwr.util.setValue("areaName", sender.province.provinceName + " " + sender.city.cityName + " " + sender.district.districtName);
        }
    }
    function insertSender() {
        if(validate($("senderForm"))) {
            var user = dwr.util.getFormValues("senderForm");
            var sender = dwr.util.getFormValues("senderForm");
            //if("${deptParentId}" == "1"){
            //    sender.deptParentId = "${deptId}";
            //    user.deptParentId = "${deptId}";
            //}
            sender.user = user;
            UserService.insertSender(sender, callback);
        }
    }
    function callback() {
        location = "${ctx}/wx/page/sender_list.jsp?userId=${userId}&deptId=${deptId}&deptCode=${deptCode}&deptParentId=${deptParentId}";
    }
</script>